﻿using Framework.Core.Consts;
using Framework.Core.Enums;
using Framework.Core.Helper;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using my.abp.Application.Contracts.Dtos.User;
using my.abp.Application.Contracts.IServices.User;
using my.abp.Domain.IManagers.User;
using my.abp.Domain.Shared.Dtos;
using my.abp.Domain.Shared.Entities;
using SqlSugar;
using System.ComponentModel;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Uow;

namespace my.abp.Application.Services.User
{
	public class SysUserService : ApplicationService, ISysUserService
	{

		private ISysUserManager _sysUserManager;
		private ISysUserRoleManager _sysUserRoleManager;
		private ISysUserExtOrgManager _sysUserExtOrgManager;
		public SysUserService(ISysUserManager sysUserManagerService, ISysUserRoleManager sysUserRoleManager, ISysUserExtOrgManager sysUserExtOrgManager)
		{
			_sysUserManager = sysUserManagerService;
			_sysUserRoleManager = sysUserRoleManager;
			_sysUserExtOrgManager = sysUserExtOrgManager;
		}

		/// <summary>
		/// 获取用户分页列表
		/// </summary>
		/// <returns></returns>
		[Authorize]
		[HttpPost]
		[DisplayName("获取用户分页列表")]
		public async Task<PagedResultDto<SysUser>> Page(PageUserInput input)
		{
			RefAsync<int> total = 0;

			var outPut = await _sysUserManager.ListAsync(input.Name, input.Account, input.OrgId,
				input.SkipCount,input.MaxResultCount, total);

			var result = new PagedResultDto<SysUser>();
			result.Items = outPut;
			result.TotalCount = total;
			return result;
		}

		/// <summary>
		/// 增加用户
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[DisplayName("增加用户")]
		[HttpPost]
		public async Task<long> AddUser(AddUserInput input)
		{
			return await _sysUserManager.AddUser(input.Adapt<SysUser>(), input.RoleIdList, input.ExtOrgIdList);
		}

		/// <summary>
		/// 更新用户
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[DisplayName("更新用户")]
		[HttpPost]
		public async Task UpdateUser(UpdateUserInput input)
		{
			await _sysUserManager.UpdateUser(input.Adapt<SysUser>(), input.RoleIdList, input.ExtOrgIdList);
		}

		/// <summary>
		/// 删除用户
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[DisplayName("删除用户")]
		[HttpPost]
		public async Task DeleteUser(DeleteUserInput input)
		{
			await _sysUserManager.DeleteUser(input.Adapt<SysUser>());
		}

		/// <summary>
		/// 查看用户基本信息
		/// </summary>
		/// <returns></returns>
		[Authorize]
		[DisplayName("查看用户基本信息")]
		public async Task<SysUser> GetBaseInfo()
		{
			return await _sysUserManager.GetBaseInfo();
		}

		/// <summary>
		/// 更新用户基本信息
		/// </summary>
		/// <returns></returns>
		[Authorize]
		[DisplayName("更新用户基本信息")]
		[HttpPost]
		public async Task<int> UpdateBaseInfo(SysUser user)
		{
			return await _sysUserManager.UpdateBaseInfo(user);
		}

		/// <summary>
		/// 设置用户状态
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[DisplayName("设置用户状态")]
		[HttpPost]
		public async Task<int> SetStatus(UserInput input)
		{
			return await _sysUserManager.SetStatus(input.Id, input.Status);
		}

		/// <summary>
		/// 授权用户角色
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[DisplayName("授权用户角色")]
		public async Task GrantRole(UserRoleInput input)
		{
			await _sysUserManager.GrantRole(input.UserId,input.RoleIdList);
		}

		/// <summary>
		/// 修改用户密码
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[DisplayName("修改用户密码")]
		[HttpPost]
		public async Task<int> ChangePwd(ChangePwdInput input)
		{
			return await _sysUserManager.ChangePwd(input.PasswordOld, input.PasswordNew);
		}

		/// <summary>
		/// 重置用户密码 
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[Authorize]
		[DisplayName("重置用户密码")]
		[HttpPost]
		public async Task<string> ResetPwd(ResetPwdUserInput input)
		{
			return await _sysUserManager.ResetPwd(input.Id);
		}

		/// <summary>
		/// 获取用户拥有角色集合
		/// </summary>
		/// <param name="userId"></param>
		/// <returns></returns>
		[Authorize]
		[DisplayName("获取用户拥有角色集合")]
		public async Task<List<long>> GetOwnRoleList(long userId)
		{
			return await _sysUserRoleManager.GetUserRoleIdList(userId);
		}

		/// <summary>
		/// 获取用户扩展机构集合
		/// </summary>
		/// <param name="userId"></param>
		/// <returns></returns>
		[DisplayName("获取用户扩展机构集合")]
		public async Task<List<long>> GetOwnExtOrgList(long userId)
		{
			return await _sysUserExtOrgManager.GetUserExtOrgList(userId);
		}

		/// <summary>
		/// 根据角色Id获取用户Id集合
		/// </summary>
		/// <param name="roleId"></param>
		/// <returns></returns>
		[Authorize]
		[DisplayName("根据角色Id获取用户Id集合")]
		public async Task<List<long>> GetUserIdList(long roleId)
		{
			return await _sysUserRoleManager.GetUserIdList(roleId);
		}
	}
}